<?php
namespace Admin\Controller;
use  Think\Controller;

class NumController extends Controller{
    // 展示一共有多少名额
    public function index(){

        //实例化Memebr
        $num = M('cutnum');
        // 导入分页类
        import('Org.Util.Page');
        $data = $num->group('userId')->field('sum(num) as number')->select();
        //查询满足满足条件的总记录条数
        $count = count($data);
        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);
        //分页显示输出
        $show = $Page->show();
        $limit = $Page->firstRow . ',' . $Page->listRows;
        $data = $num->group('userId')->field('sum(num) as number,userId')->limit($limit)->order('number desc')->select();


        foreach($data as $k=>$v){

            $ids[] = $v['userid'];
        }

        $ids1 = implode(',',$ids);

        if($ids1){

            $res_user = M('user')->where('id in ('.$ids1.')')->select();
        }

        foreach($res_user as $k=>$v){

            if($v['level'] == 3){

                $list_id1[] = $v['detailid'];
            }

            if($v['level'] == 4){

                $list_id2[] = $v['detailid'];
            }
        }

        $list_id1 = implode(',',$list_id1);
        $list_id2 = implode(',',$list_id2);

        if($list_id1){
            $res_detail1 = M('detail3')->where('id in ('.$list_id1.')')->select();
        }

        if($list_id2){
            $res_detail2 = M('detail4')->where('id in ('.$list_id2.')')->select();
        }

        foreach($data as $k=>$v){
            foreach($res_detail1 as $k1=>$v1){

                if($v['userid'] == $v1['userid']){
                    $data[$k]['phone'] = $v1['phone'];
                    $data[$k]['regtime'] = $v1['regtime'];
                }
            }

            foreach($res_detail2 as $k2=>$v2){

                if($v['userid'] == $v2['userid']){
                    $data[$k]['phone'] = $v2['phone'];
                    $data[$k]['regtime'] = $v2['regtime'];

                }
            }

            foreach($res_user as $k3=>$v3){

                if($v['userid'] = $v3['id']){

                    $data[$k]['user'] = $v3['user'];
                    $data[$k]['sex'] = $v3['sex'];
                    $data[$k]['level'] = $v3['level'];
                }
            }

        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['regtime'] = date('Y-m-d',$v['regtime']);
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function edit(){
        if(IS_AJAX){
            $uid = I('userid');
            $num = I('num');

            $arr_num['userId'] = $uid;
            $arr_num['num'] = $num;
            $arr_num['geterId'] = 0;

            $res_num = M('cutnum')->add($arr_num);
            if($res_num){

                $this->ajaxReturn(['info'=>'新增成功','status'=>1,'url'=>U('Num/index')]);
            }else{
                $this->ajaxReturn(['info'=>'新增成功','status'=>1,'url'=>U('Num/edit')]);
            }
        }else{

            $uid = I('id');
            $where['userId'] = $uid;
            $num = M('cutnum');

            $data = $num->where($where)->field('sum(num) as number,userId')->select();

            $res_user = M('user')->where('id = "'.$uid.'"')->find();

            $list_id1 = $res_user['detailid'];

            $table1 = 'detail'.$res_user['level'];

            $res_detail1 = M($table1)->where('id = "'.$list_id1.'"')->find();

            foreach($data as $k=>$v){

                if($v['userid'] = $res_detail1['userid']){
                    $data[$k]['phone'] = $res_detail1['phone'];
                    $data[$k]['regtime'] = $res_detail1['regtime'];
                }

                if($v['userid'] = $res_user['id']){
                    $data[$k]['user'] = $res_user['user'];
                    $data[$k]['sex'] = $res_user['sex'];
                    $data[$k]['level'] = $res_user['level'];
                }


            }

            $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

            foreach($data as $k=>$v){

                $data[$k]['level'] = $getlevel[$v['level']];
                $data[$k]['regtime'] = date('Y-m-d',$v['regtime']);
            }

            $list = $data[0];

//            var_dump($list);die;

            $this->assign('member',$list);
            $this->display();
        }

    }
}